import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///
/// [author]: liMing
/// [Date]: 2021-07-14  20:46
///
class PagePainter extends CustomPainter{
  final double step = 20; // 小格边长
  var _gridPint;
  @override
  void paint(Canvas canvas, Size size) {
    var paint=Paint()
        ..color=Colors.blue
        ..style=PaintingStyle.fill;
    canvas.translate(size.width/2, size.height/2);
    canvas.drawCircle(Offset(0,0), 50, paint);
    var paint1=Paint()
      ..color=Colors.black
      ..style=PaintingStyle.fill
    ..strokeWidth=3;
    canvas.drawLine(Offset(-size.width/2, 0), Offset(size.width/2,  0), paint1);
    canvas.drawLine(Offset(0, -size.height/2), Offset(0,  size.height/2), paint1);
    ///////方格
     _gridPint=Paint()..color=Colors.grey..strokeWidth=.5;

    _drawGrid(canvas,size);

    _drawDot(canvas,size);


  }
  ///画方格
  void _drawBottomRight(Canvas canvas,Size size){
    canvas.save();
    for(int i=0;i<size.height/2/step;i++){
      canvas.drawLine(Offset(0,0), Offset(size.width/2,0), _gridPint);
      canvas.translate(0, step);
    }
    canvas.restore();

    canvas.save();
    for(int i=0;i<size.width/2/step;i++){
      canvas.drawLine(Offset(0,0), Offset(0,size.height/2), _gridPint);
      canvas.translate(step, 0);
    }
    canvas.restore();
  }
  ///画方格
  void _drawGrid(Canvas canvas,Size size,){
    _drawBottomRight(canvas,size);
    canvas.save();
    canvas.scale(1,-1);//沿X轴 -y镜像
    _drawBottomRight(canvas,size);
    canvas.restore();

    canvas.save();
    canvas.scale(-1,1);//沿-y轴 x镜像
    _drawBottomRight(canvas,size);
    canvas.restore();

    canvas.save();
    canvas.scale(-1,-1);//沿原点镜像
    _drawBottomRight(canvas,size);
    canvas.restore();
  }

  void _drawDot(Canvas canvas,Size size){
    var paint=Paint()..color=Colors.orangeAccent..strokeWidth=4;
    var count=12;
    canvas.save();
    for(int i=0;i<12;i++){
      var step=2*pi/count;
      canvas.drawLine(Offset(0,60), Offset(0,90), paint);
      canvas.rotate(step);
    }
    canvas.restore();
    canvas.drawLine(Offset(0,0), Offset(20,-100), paint..color=Colors.red);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }

}